package com.source.hotelbath;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.source.hotelbath.base.BaseFragment;
import com.source.hotelbath.base.BizActivity;
import com.source.hotelbath.fragment.LinenFragment;
import com.source.hotelbath.fragment.MineFragment;
import com.source.hotelbath.fragment.OrderFragment;
import com.source.hotelbath.utils.SdkTools;
import com.source.hotelbath.weight.radiogp.RadioGp;
import com.source.hotelbath.weight.radiogp.RadioItem;

import java.util.List;

import cn.weipass.pos.sdk.impl.WeiposImpl;

public class MainActivity extends BizActivity {


    private FrameLayout fl_content;
    private RadioGp rgp_content;
    private RadioItem ri_get;
    private RadioItem ri_order;
    private RadioItem ri_mine;

    @Override
    protected void onBaseCreate(Bundle var1) {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void bindView(Bundle var1) {
        SdkTools.initSdk(this);
        fl_content = (FrameLayout) findViewById(R.id.fl_content);
        rgp_content = (RadioGp) findViewById(R.id.rgp_content);
        ri_get = (RadioItem) findViewById(R.id.ri_get);
        ri_order = (RadioItem) findViewById(R.id.ri_order);
        ri_mine = (RadioItem) findViewById(R.id.ri_mine);

        rgp_content.setcheck(ri_get.getId());
        showFragmentByTag(LinenFragment.TAG);

        rgp_content.setOnCheckChangeListener(new RadioGp.OnCheckChangeListener() {
            @Override
            public boolean onCheckChaenged(int id) {
                if (id == ri_get.getId()) {

                    showFragmentByTag(LinenFragment.TAG);
                    return true;
                } else if (id == ri_order.getId()) {

                    showFragmentByTag(OrderFragment.TAG);
                    return true;
                } else if (id == ri_mine.getId()) {

                    showFragmentByTag(MineFragment.TAG);
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        boolean success=intent.getBooleanExtra("success",false);
        if (success){
            rgp_content.setcheck(ri_order.getId());
            showFragmentByTag(OrderFragment.TAG);
        }
    }

    private void showFragmentByTag(String tag) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);

        if (null != fragments) {
            for (int i = 0; i < fragments.size(); i++) {
                if (null == fragments.get(i)  )
                    continue;
                ft.hide(fragments.get(i));
                Log.d("fragments.get(i)",i+"----");
            }
            if (null == fragment) {
                fragment = FragmentFactory.getInstance().getFragmentByTag(getSupportFragmentManager(), tag);
                ft.add(R.id.fl_content, fragment, tag);
            }
            ft.show(fragment);
            ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
            getSupportFragmentManager().executePendingTransactions();
        } else {
            if (null == fragment) {
                fragment = FragmentFactory.getInstance().getFragmentByTag(getSupportFragmentManager(), tag);
                ft.add(R.id.fl_content, fragment, tag);
                ft.show(fragment);
                ft.addToBackStack(null);
                ft.commitAllowingStateLoss();
                getSupportFragmentManager().executePendingTransactions();
            }
//            Log.d(t, "fragments null!");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            WeiposImpl.as().destroy();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
